var layer;
var time = 2000;
var postTime = 2000;

layui.use('layer', function(){
    layer = layui.layer;
});

//菜单跳转
function menu_sub(url,data) {
    $.ajax({
        url:url,
        type:'GET', //GET
        data:data,
        beforeSend:function(xhr){
            layer.closeAll();
            layer.load(2, {
                shade: [0.1,'#fff']
                ,time:0
            });
        },
        success:function(data,textStatus,jqXHR){
            if(data.msg){
                layer.closeAll('loading');
                layer.msg(data.msg,{time:3000});
            }else {
                $('.layui-body').html(data);
            }
        },
        error:function(xhr,textStatus){
            layer.msg('网络可能不太给力，请重试！',{time:time});
        },
        complete:function(){
            layer.closeAll('loading');
        }
    })
}

//提交跳转
function post_sub(url,data,table,is_close) {
    is_close = is_close !== false ? true : false;
    var QQIndex;
    $.ajax({
        url:url,
        type:'POST',
        data:data,
        dataType:'json',
        beforeSend:function(xhr){
            QQIndex = layer.msg('请求中', {
                icon: 16
                ,shade: 0.01
                ,time:0
            });
        },
        success:function(data,textStatus,jqXHR){
            layer.msg(data.msg,{time:postTime,success:function () {
                if(data.code == 0) {
                    if (table) {
                        table.reload('testReload');
                    }
                    if(is_close) {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }
                    if(!isNull(data.url)){
                        if(is_close) {
                            parent.menu_sub(data.url);
                        }else{
                            menu_sub(data.url);
                        }
                    }
                }
            }});
        },
        error:function(xhr,textStatus){
            layer.msg('网络可能不太给力，请重试！',{time:time});
        },
        complete:function(){
            layer.close(QQIndex);
        }
    })
}

/**
 * 打开窗口
 * @param url
 * @param data
 * @param title
 * @param w
 * @param h
 * @param z
 */
function layui_open(url,data,title,w,h,table,z,maxmin,is_table) {
    z = z ? z : '19891014';//弹出层置顶数
    w = w ? w : '65%';//宽度
    h = h ? h : '80%';//高度
    maxmin = maxmin ? maxmin : false;
    is_table = is_table !== false ? true : false;
    title = title ? title : '弹出层';
    if(data){
        var urlTwo = '?';
        $.each(data, function(i, item){
            urlTwo += i + '=' + item + "&";
        });
        urlTwo=urlTwo.substring(0,urlTwo.length-1);
        url += urlTwo;
    }
    var openIndex = layer.open({
        type: 2,
        title:title,
        area: [w, h],
        skin: 'layui-layer-rim',
        fixed: true, //不固定
        maxmin: maxmin,
        resize:false,
        zIndex:z,
        content: url,
        end:function () {
            if(table) {
                table.reload('testReload');
            }
        },
        cancel: function(index, layero){
            if(is_table) {
                table = false;
            }
        }
    });
}

//获取列表加载
function address_post_sub(url,data,divs,form,dizhi) {
    $.ajax({
        url:url,
        type:'POST',
        data:data,
        dataType:'json',
        success:function(data,textStatus,jqXHR){
            if(data.msg){
                layer.msg(data.msg,{time:time})
            }else{
                var html = '<option value="">请选择</option>';
                $.each(data,function (index,element) {
                    var str = '';
                    if(dizhi != ''){
                        if(parseInt(element.id) == parseInt(dizhi)){
                            str = 'selected';
                        }
                    }
                    html = html +  '<option value="'+element.id+'" '+str+'>'+element.name+'</option>';
                });
                divs.html(html);
                form.render('select');
            }
        },
        error:function(xhr,textStatus){
            layer.msg('网络开了小猜！',{time:time});
            console.log('错误')
            console.log(xhr)
            console.log(textStatus)
        },
    })
}

/**
 * 验证手机号码是否正确
 * @param poneInput
 * @returns {boolean}
 */
function isPoneAvailable(poneInput) {
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(poneInput)) {
        return false;
    } else {
        return true;
    }
}

/**
 * 验证是不是数字
 * @param poneInput
 * @returns {boolean}
 */
function isNumber(poneInput) {
    var myreg=/^[0-9]*$/;
    if (!myreg.test(poneInput)) {
        return false;
    } else {
        return true;
    }
}

/**
 * 检查变量是否为空
 * @param data
 * @returns {boolean}
 */
function isNull(data){
    return (data == "" || data == undefined || data == null) ? true : false;
}

/**
 * 检查变量是否为数组
 * @param o
 * @returns {boolean}
 */
function isArray(o){
    return Object.prototype.toString.call(o)=='[object Array]';
}